package com.shuhe.timer;

import com.shuhe.JettyServer;
import com.shuhe.common.MyDateUtils;
import com.shuhe.common.Tools;
import com.shuhe.domain.mxgc.TaskCron;
import com.shuhe.metastore.MetaMysql;

import java.sql.Connection;
import java.util.*;

import static com.shuhe.common.CommonFunction.closeDb;

public class MyTaskDelLog extends TimerTask {
    private static boolean isRunning = false;

    public MyTaskDelLog() {
    }

    public void run() {
        Calendar cal = Calendar.getInstance();
        if (!isRunning) {
            if (true) {
                isRunning = true;
                System.out.println("开始执行指定任务----------日志删除开始");
                Connection conn = null;
                try {
                    conn = MetaMysql.connect();
                    //获取前10天的时间
                    String time10Before =
                            MyDateUtils.getBeforeDayBydate(MyDateUtils.getDataString(new Date()), -20).substring(0, 10);
                    String sql = "";
                    ArrayList<String> sqls = new ArrayList<>();


                    //插历史
                    sql = "INSERT INTO mxgc_user_task_log_sjjr_history (batch_id,count,cron_fun,error_msg," +
                            "run_endtime," + "run_starttime,run_status,task_id,task_name) \n" + "SELECT batch_id," +
                            "count,cron_fun,error_msg,run_endtime,run_starttime,run_status,task_id," + "task_name " +
                            "FROM mxgc_user_task_log_sjjr WHERE run_starttime<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "INSERT INTO mxgc_user_task_log_sjjr_zt_history (batch_id,nyr,update_time,zt)\n" + "SELECT " +
                            "batch_id,nyr,update_time,zt FROM mxgc_user_task_log_sjjr_zt WHERE update_time<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "INSERT INTO mxgc_user_task_log_history (error_msg,qssj,sjl,STATUS,task_id,task_name," +
                            "task_pc,task_rule,zzsj)\n" + "SELECT error_msg,qssj,sjl,STATUS,task_id,task_name," +
                            "task_pc,task_rule,zzsj FROM " + "mxgc_user_task_log WHERE qssj<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "INSERT INTO service_log_history (err_detail,id,req_body,req_time,res_body,res_status," +
                            "res_time,service_code,user_name)\n" + "SELECT err_detail,id,req_body,req_time,res_body," +
                            "res_status,res_time,service_code," + "user_name FROM service_log WHERE req_time<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "INSERT INTO klj_rwxx_xq_history (plrwid,plrwmc,rwfl,rwid,rwmc,sjl,updatetime,zxqsrq,zxzt," + "zxzzrq)\n"
                            + "SELECT plrwid,plrwmc,rwfl,rwid,rwmc,sjl,updatetime,zxqsrq,zxzt,zxzzrq FROM klj_rwxx_xq " + "WHERE zxqsrq<'" + time10Before + "'\n";
                    sqls.add(sql);

                    //删原表
                    sql = "delete from mxgc_user_task_log_sjjr where run_starttime<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "delete from mxgc_user_task_log_sjjr_zt where update_time<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "delete from mxgc_user_task_log where qssj<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "delete from service_log where req_time<'" + time10Before + "'\n";
                    sqls.add(sql);
                    sql = "delete from klj_rwxx_xq where zxqsrq<'" + time10Before + "'\n";
                    sqls.add(sql);

                    for (int i = 0; i < sqls.size(); i++) {
                        MetaMysql.execSql(conn, sqls.get(i));
                    }


                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    closeDb(conn);
                }
                isRunning = false;
                System.out.println("开始执行指定任务----------日志删除结束");
            }
        } else {
            System.out.println("上一次任务执行还未结束");
        }
    }

}

